package com.sskj.gcc.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.gcc.bean.LoginBean;
import com.sskj.gcc.http.HttpConfig;
import com.sskj.gcc.ui.activity.LoginActivity;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.m.bean.LoginPreBean;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.lib.Constants;
import com.sskj.lib.http.JsonCallBack;


public class LoginActivityPresenter extends BasePresenter<LoginActivity> {


    /**
     * 登录前接口
     *
     * @param mobile 账号
     * @param pws    密码
     */
    public void loginPre(String mobile, String pws) {
        OkGo.<HttpData<LoginPreBean>>post(HttpConfig.BASE_URL + com.sskj.gocoin.m.HttpConfig.LOGIN_PRE)
                .params("username", mobile)
                .params("password", MD5Util.getMd5Value(pws))
                .execute(new JsonCallBack<HttpData<LoginPreBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<LoginPreBean>> response) {
                        HttpData<LoginPreBean> httpListData = response.body();
                        if (httpListData.getCode() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_MOBILE, mobile);
                            SPUtils.put(MyAppLication.getApplication(), Constants.GOOGLETYPE, httpListData.getData().getSta());
                            //重新初始化公共头
                            mView.loginPreSuccess(httpListData.getData().getSta());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }


    /**
     * 密码登录
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @return
     */
    public void login(String mobile, String opwd, String google_code) {

        httpService.login(mobile, opwd, google_code).execute(loginCallBack);
    }

    JsonCallBack<HttpData<LoginBean>> loginCallBack = new JsonCallBack<HttpData<LoginBean>>(this) {
        @Override
        public void onSuccess(Response<HttpData<LoginBean>> response) {
            HttpData<LoginBean> httpData = response.body();
            if (httpData.getCode() == HttpConfig.OK) {
                mView.loginSuccess(httpData.getData());
            }
        }
    };

    public void checkGoogle(String code, String account,String password) {

        login(account,password,code);

//        httpService.checkGoogle(code)
//                .execute(new JsonCallBack<HttpData>(this) {
//                    @Override
//                    public void onSuccess(Response<HttpData> response) {
//                        HttpData httpData = response.body();
//                        if (httpData.getCode() == HttpConfig.OK) {
//                            mView.loginSuccess(loginBean);
//                        }
//                    }
//                });
    }

//    public void sendCode(String account) {
//        if (account.contains("@")) {
//            sendEmail(account);
//        } else {
//            sendSms(account, "3");
//        }
//    }

    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
//    public void sendSms(String mobile, String type) {
//
//        httpService.sendSms(mobile, type).execute(callback);
//    }

//    JsonCallBack<HttpData> callback = new JsonCallBack<HttpData>(this) {
//        @Override
//        public void onSuccess(Response<HttpData> response) {
//            HttpData httpData = response.body();
//            if (httpData.getCode() == HttpConfig.OK) {
//                mView.sendCheckCodeSuccess();
//            } else {
//                ToastUtil.showShort(httpData.getMsg());
//                mView.sendCheckCodeFail();
//            }
//        }
//
//        @Override
//        public void onError(Response<HttpData> response) {
//            mView.sendCheckCodeFail();
//            super.onError(response);
//        }
//    };

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
//    public void sendEmail(String email) {
//        httpService.sendEmail(email, null).execute(callback);
//    }


    /**
     * 验证码登录
     *
     * @param account
     * @param code
     */
    public void loginCode(String account, String code) {

        httpService.loginCode(account, code).execute(loginCallBack);
    }
}
